package ncov.dao.impl;

import ncov.dao.GradeDao;
import ncov.model.Grade;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author WuChangJian
 * @date 2020/6/5 17:51
 */
@Repository
public class GradeDaoImpl implements GradeDao {

    @Autowired
    private SessionFactory sessionFactory;
    private Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    @Override
    public Grade queryById(Integer gradeId) {
        Session session = getSession();
        return session.get(Grade.class, gradeId);
    }

    @Override
    public List<Grade> queryAllByLimit(int offset, int limit) {
        return null;
    }

    @Override
    public List<Grade> queryAll(Grade grade) {
        /*Criteria criteria = getSession().createCriteria(Grade.class);
        if (grade.getGradeName() != null) {
            criteria.add(Restrictions.like("gradeName", grade.getGradeName()));
        }
        if (grade.getStaff() != null && grade.getStaff().getStaffId() != null) {
            criteria.add(Restrictions.eq("staff.staffId", grade.getStaff().getStaffId()));
        }
        criteria.addOrder(Order.asc("gradeId"));*/
        String sql = "FROM Grade g where 1= 1 ";

        if (grade.getGradeName() != null) {
            sql = sql + " and g.gradeName=" + grade.getGradeName();
        }
        if (grade.getStaff() != null && grade.getStaff().getStaffId() != null) {
            sql = sql + " and g.staff.staffId=" + grade.getStaff().getStaffId();
        }
        Query query = getSession().createQuery(sql);
        return query.list();
    }

    @Override
    public int insert(Grade grade) {
        return 0;
    }

    @Override
    public int update(Grade grade) {
        return 0;
    }

    @Override
    public int deleteById(Integer gradeId) {
        return 0;
    }
}
